<style type="text/css">
  #iframe_pay {
    border: none;
  }

  .step_footer {
    display: none !important;
  }

  .nnpay_accept_btn {
    display: block;
    height: 46px;
    line-height: 46px;
    text-align: center;
    border-radius: 4px;
    background: #149EE3;
    text-align: center;
    font-size: 16px;
    color: #fff;
    border: 0;
    width: 100%;
    cursor: pointer;
    font-weight: bold;
  }

  .nnpay_accept_btn[disabled] {
    background: #ccc
  }
</style>
<iframe src="$payUrl" name="iframe_pay" id="iframe_pay" scrolling="no" id="iframe_pay"
  height="355px" width="100%"></iframe>
<button type="submit" class="nnpay_accept_btn"  id="submit" disabled><span
    class="nc_text" key_name="button">Pay</span></button>
<script src="/payments/layer/layer.js"></script>
<script>
  $(function () {

    let cardKey = "";

    $("#submit").click(async function () {

      const oemBeforePaymentResults = window["oemBeforePayment"] ? await window["oemBeforePayment"]() : true;
      if(!oemBeforePaymentResults){
        return false
      }

      moi.appendLoading({ theme: 'light', container: '#submit' })
      $.ajax({
        url: '/homeapi/checkouts/payment/nnpay/createOrder?order_number={$order->order_number}&card_key=' + cardKey,
        type: 'get',
        success: function (res) {
          moi.removeLoading(".container")
          var obj = JSON.parse(res);
          if (obj.code == 2 && obj.url !== undefined) {
            layer.open({
              title: '',
              type: 2,
              area: ['90%', '90%'],
              content: [obj.url, 'no'],
              closeBtn: 0,
            });
          } else {
            location.href = "/checkouts/clientcallback/nnpay?order_number={$order->order_number}" + "&code=" + obj.code + "&msg=" + obj.msg + '&data=' + encodeURIComponent(JSON.stringify(obj));
          }
        }
      })
    })

    //监听card页面通知消息
    window.addEventListener('message', function (event) {
      let eventType = event.data.eventType;
      var k = event.origin || event.originalEvent.origin
      if (eventType === 'parameter') {
        document.getElementById('iframe_pay').contentWindow.postMessage({
          eventType: 'GetCardKey',
          // appId、appSecret、ts、sign自定义填写
          appId: '$api_id',
          appSecret: '$api_key',
          merchantNo: '$merchantNo',
          sign: '$sign',
          // 不可更改
          domainUrl: k
        }, '*');
      }
      if (eventType === 'GetCardKeySuccess') {
        $('.nnpay_accept_btn').attr('disabled', false)
        cardKey = event.data.cardKey
      } else if (eventType === 'GetCardKeyError') {
        moi.message({content:event.data.message,type:"error"})
      } else if (eventType === 'VerifySecretKeySuccess') {
      } else if (eventType === 'VerifySecretKeyError') {
        moi.message({content:event.data.message,type:"error"})
      } else if (eventType === 'NotifyErrorMsg') {
        moi.message({content:event.data.message,type:"error"})
      }
      console.log("从" + event.origin + "收到消息： " + event.data)
      console.log("子级域名：" + k)
      console.log(event.data)
      console.log('进来')
    }, false);
  })


</script>